export const formatDate = function (date = new Date()) {
    let year = date.getFullYear();
    let month = (date.getMonth() + 1) ;
    let day = date.getDate();
    let week = ['天', '一', '二', '三', '四', '五', '六'][date.getDay()];

    const dateInfo = {
        date: new Date(date),
        dateStr: `${year}-${month.toString().padStart(2,'0')}-${day.toString().padStart(2,'0')}`,
        simpleDateStr: `${month.toString().padStart(2,'0')}-${day.toString().padStart(2,'0')}`,
        year,
        month,
        day,
        week,
        isToday:false
    }

    const today = new Date();
    // 判断是否为当天
    if (today.getFullYear() === year && (today.getMonth() + 1) === month && today.getDate() === day) {
        dateInfo['isToday'] = true;
    }
    return dateInfo;
};
export const setDate = function (date = new Date(), step = 7) {
    let weekData = [];
    const week = date.getDay() - 1;
    date = getDateByDate(date, week * -1);
    for (let i = 0; i < step; i++) {
        weekData.push(formatDate(i == 0 ? date : getDateByDate(date, 1)))
    }
    return weekData;
};

// 根据基准日期获取前后某天的日期对象
export const getDateByDate = function (date = new Date(), range = 0) {
    date.setDate(date.getDate() + range);
    return date;
};

export const getWeekDate = ({baselineDate = new Date(), range = 0, step = 7}) => {
    return setDate(getDateByDate(baselineDate, range), step);
};

export function currentWeek(date) {
    const dateFormat = (date) => {
        let year = date.getFullYear();
        let month = (date.getMonth() + 1) ;
        let day = date.getDate();

      return year + '-' + (month < 10 ? ('0' + month) : month) + '-' + (day < 10 ? ('0' + day) : day)
    }
    
    //一天的毫秒数
    const ondDayTime = 86400000
    //-1是因为要把当天的毫秒数去掉
    let day = date.getDay() - 1
    let firstDay = ''
    //如果<0，就是周末，所以取6
    day < 0 ? day = 6 : ''
    //算出周一是哪一天
    firstDay = new Date(dateFormat(new Date(date.getTime() - (day * ondDayTime))))
    const week = []
    const chineseWeek = ['一', '二', '三', '四', '五', '六', '天']
    for (let i = 0; i <= 6; i++) {
      week.push({
           //可换成星期
        chinese: '周' + chineseWeek[i],
        day: i,
        date: dateFormat(new Date(firstDay.getTime() + (i * ondDayTime)))
      })
    }
    return week //结果
}

export function getServiceDates(date = new Date(), days) {
    const dates = [];
    for (let i = 0; i < days; i++) {
        // 获取当前日期的时间戳
        let timestamp = date.getTime();

        // 计算 i 天前的时间戳
        let dayTimestamp = 24 * 60 * 60 * 1000; // 一天的毫秒数
        let iDayAgoTimestamp = timestamp + i * dayTimestamp;

        // 转换为日期对象
        let newDate = new Date(iDayAgoTimestamp);
        dates.push(formatDate(newDate));
    }
    return dates;
}

export function getServiceTimes(startHour, endHour) {
    let startHourInt = parseInt(startHour)
    let endHourInt = parseInt(endHour)
    const times = [];
    for (let i = startHourInt; i <= endHourInt; ++i) {
        let hour = i < 10 ? ('0' + i) : i
        if (i != endHour) {
            times.push(hour + ":00");
            times.push(hour + ":30");
        } else {
            times.push(hour + ":00");
        }
    }

    return times;
}

export function getDateTimeDiff(startTime, endTime) {
    var startTimeMills = new Date(startTime.replace(/-/g,'/')).getTime()
    var endTimeMills = new Date(endTime.replace(/-/g,'/')).getTime()

    var result = (endTimeMills - startTimeMills) / 60 / 1000
    return result
}